<?php

namespace Home\Controller;

class StoreController extends BaseStoreController {

    function _initialize() {
        parent::_initialize();
        $this->_Store_mod = M("Store");
        $this->_Goods_mod = M("Goods");
    }

    function check_param() {
        $this->_store_id = I('request.store_id');
        if (!$this->_store_id) {
            render('10201', '店铺ID为空');
        }
    }

    /**
     * @api {get} /index.php?m=Home&c=Store&a=index 店铺首页
     */
    function index() {
        $this->check_param();
        $store_id = $this->_store_id;
        $store_info = D('store')->get_info($store_id);
        if (empty($store_info)) {
            render('10200', '店铺不存在');
        }

        /* 取得推荐商品 */
        $recommended_goods = M('Goods')->where("store_id=" . $store_id . " AND closed = 0 AND if_show = 1 AND recommended = 1")->limit(12)->order('add_time desc')->select();
        /* 取得最新商品 */
        $new_goods = M('Goods')->where("store_id=" . $store_id . " AND closed = 0 AND if_show = 1")->limit(12)->order('add_time desc')->select();
        /* 取得热卖商品 */
        $hot_goods = M('Goods')->alias('goods')->join("LEFT JOIN " . C('DB_PREFIX') . 'goods_statistics goods_statistics ON goods_statistics.goods_id=goods.goods_id')->where("store_id=" . $store_id . " AND closed = 0 AND if_show = 1")->limit(12)->order('add_time desc')->select();

        render('10000', '', array('store_info' => $store_info, 'recommended_goods' => $recommended_goods, 'new_goods' => $new_goods, 'hot_goods' => $hot_goods));
    }


    /**
     * @api {get} /index.php?m=Home&c=Store&a=info 获取店铺的基本信息
     */
    function info() {
        $this->check_param();
        $store_id = $this->_store_id;
        $store_info = $this->get_store_data($store_id);
        render('10000', '', $store_info);
    }

    /**
     * @api {get} /index.php?m=Home&c=Store&a=category_goods 获取店铺商品分类
     */
    function category_goods() {
        $this->check_param();
        $scategories = D("Gcategory")->get_list(-1, true, $this->_store_id);
        import("Org.Com.Tree");
        $tree = new \Org\Com\Tree;
        $tree->setTree($scategories, 'cate_id', 'parent_id', 'cate_name');
        $data = $tree->getArrayList(0);
        render('10000', 'OK', array('category' => $data));
    }

    /**
     * @api {get} /index.php?m=Home&c=Store&a=search_goods 获取店铺的所有商品
     */
    function search_goods() {
        $this->check_param();
        $store_id = $this->_store_id;
        $map['store_id'] = $store_id;
        $map['closed'] = 0;
        $map['if_show'] = 1;

        $keyword = I('request.keyword', '');
        $cate_id = I('request.cate_id');
        if ($keyword) {
            $map['goods_name'] = array('LIKE', '%' . $keyword . '%');
        }
        if ($cate_id) {
            $goods_ids = M('CategoryGoods')->where(array('cate_id' => array('IN', D('Gcategory')->get_descendant($cate_id, $store_id))))->getField('goods_id', true);
            if ($goods_ids) {
                $map['g.goods_id'] = array('IN', $goods_ids);
            }else{
                $map['g.goods_id'] = '';
            }
        }
        
        //排序
        $sort = I('request.sort', '');
        $order = I('request.order', '');
        if (!in_array($sort, array('sales', 'price', 'add_time', 'recommended'))) {
            $sort = 'sales';
        }
        if (!in_array($order, array('desc', 'asc'))) {
            $order = 'desc';
        }
        
        
        $goods_list = $this->_Goods_mod->alias('g')
                ->field('g.goods_id,goods_name,market_price,price,default_image,comments,sales')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods_statistics gs ON gs.goods_id = g.goods_id')
                ->where($map)
                ->order('add_time desc')
                ->order($sort.' '.$order)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        $data = array(
            'goods_list' => $goods_list,
        );
        render('10000', '成功', $data);
    }

    /**
     * @api {get} /index.php?m=Home&c=Store&a=coupon_list 获取店铺的所有优惠券
     * @apiName 获取店铺的所有优惠券
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取店铺的所有优惠券
     * 
     * @apiParam {String} store_id 店铺ID
     * 
     * @apiSuccess {String} coupon_id 优惠券ID
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} coupon_name 优惠券名称
     * @apiSuccess {String} coupon_value 优惠券金额
     * @apiSuccess {String} use_times 可使用次数
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} min_amount 满多少使用
     * @apiSuccess {String} if_issue 是否可用
     * @apiSuccess {String} coupon_bg 优惠券背景
     * @apiSuccess {String} mall_recommended 是否推荐
     * 
     */
    function coupon_list() {
        $this->check_param();
        $map['store_id'] = $this->_store_id;
        $map['if_issue'] = 1;
        $map['end_time'] = array('gt', time());
        $coupon_mod = M('coupon');
        $coupon_list = $coupon_mod
                ->field('coupon_id,store_id,coupon_name,coupon_value,use_times,start_time,end_time,min_amount,if_issue,coupon_bg,mall_recommended')
                ->where($map)
                ->select();
        
        if($coupon_list){
            foreach ($coupon_list as $key => $coupon) {
                $coupon_list[$key]['start_time'] = date('Y-m-d', $coupon['start_time']);
                $coupon_list[$key]['end_time'] = date('Y-m-d', $coupon['end_time']);
            }
        }
        
        render('10000', '', array('coupon_list'=>$coupon_list));
    }
    /**
     * @api {get} /index.php?m=Home&c=Store&a=coupon_detail 获取店铺的优惠券详情
     * @apiName 获取店铺的所有优惠券
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取店铺的所有优惠券
     * 
     * @apiParam {String} store_id 店铺ID
     * @apiParam {String} coupon_id 优惠券ID
     * 
     * @apiSuccess {String} coupon_id 优惠券ID
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} coupon_name 优惠券名称
     * @apiSuccess {String} coupon_value 优惠券金额
     * @apiSuccess {String} use_times 可使用次数
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} min_amount 满多少使用
     * @apiSuccess {String} if_issue 是否可用
     * @apiSuccess {String} coupon_bg 优惠券背景
     * @apiSuccess {String} mall_recommended 是否推荐
     */
    function coupon_detail(){
        $this->check_param();
        $map['store_id'] = $this->_store_id;
        
        $coupon_id  = I('request.coupon_id',0,'intval');
        if(empty($coupon_id)){
            render('10201','优惠券ID为空');
        }
        $map['coupon_id'] = $coupon_id;
        
        $map['if_issue'] = 1;
        $coupon_mod = M('coupon');
        $coupon = $coupon_mod
                ->field('coupon_id,store_id,coupon_name,coupon_value,use_times,start_time,end_time,min_amount,if_issue,coupon_bg,mall_recommended')
                ->where($map)
                ->find();
        render('10000', '', array('coupon'=>$coupon));
    }
    
    /**
     * @api {get} /index.php?m=Home&c=Store&a=get_shipping_list 获取店铺的所有物流方式
     * @apiName 获取店铺的所有物流方式
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取店铺的所有物流方式
     * 
     * @apiParam {String} store_id 店铺ID
     * 
     * @apiSuccess {String} shipping_id 物流方式ID
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} shipping_name 物流方式名称
     * @apiSuccess {String} shipping_desc 物流描述
     * @apiSuccess {String} first_price 首件价格
     * @apiSuccess {String} step_price 附加价格
     * @apiSuccess {String} cod_regions 可货到付款地区
     * @apiSuccess {String} enabled 是否开启
     * @apiSuccess {String} sort_order 排序
     * @apiSuccess {String} cod_regions_price 地区价位
     * 
     */
    function get_shipping_list()
    {
        $store_id = I('request.store_id',0,'intval');
        if(!$store_id){
            render('10201','店铺ID为空');
        }
        $map['store_id'] = $store_id;
        $map['enabled'] = 1;
        $shipping_list = M('shipping')->where($map)->select();
        render('10000', '', array('shipping_list'=>$shipping_list));
    }
}
